package com.lawliet.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lawliet.example.model.system.SysPermission;
import com.lawliet.example.model.system.SysUser;
import com.lawliet.example.model.vo.UserDetailsImpl;
import com.lawliet.example.repository.system.SysUserMapper;
import com.lawliet.example.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//用于拆分UserServiceImpl功能的
@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserMapper userMapper;


    /**
     * 判断用户信息填写是否正确
     * @param username 不一定是用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //权限
        List<SysPermission> permissionList = new ArrayList<>();
        LambdaQueryWrapper<SysUser> con = new LambdaQueryWrapper<>();
        con.eq(SysUser::getUsername,username);
        SysUser userInfo = userMapper.selectOne(con);
        if (userInfo == null) {
            throw new UsernameNotFoundException("找不到用户："+username);
        }else {
            //查询当前用户对应的角色code
            permissionList = userMapper.getPermissionByUserName(userInfo.getId());
        }
        return new UserDetailsImpl(userInfo,permissionList);
    }
}
